package tragame.game;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import tragame.game.BuildingProd.BuildingProdType;
import tragame.game.BuildingStorage.BuildingStorageType;
import tragame.interfaces.IBuilding;

public class Building extends UnicastRemoteObject implements IBuilding{
	
	private static final long serialVersionUID = 1L;
	
	private int status;
	private int level;
	private int costWood;
	private int costSoil;
	private int costIron;
	private int costFood;
	private int constructionTime;
	private int remainingTime;
	
	private BuildingType type;	
	public enum BuildingType{
		Hotel, Scierie, Mine, CARRIERE, Ferme, Entrepot, Silo, Caserne, Ecurie;
		public BuildingProdType getBuildProdType() {
			switch(this) {
				case Ferme:
					return BuildingProdType.Ferme;
				case CARRIERE:
					return BuildingProdType.CARRIERE;
				case Mine:
					return BuildingProdType.Mine;
				default:
					return BuildingProdType.Scierie;
			}
		}		
		public BuildingStorageType getBuildStorageType() {
			switch(this) {
				case Silo:
					return BuildingStorageType.Silo;
				default:
					return BuildingStorageType.Entrepot;
			}
		}
	}
	
	/**
	 * This method builds any building with default settings
	 * @param type of the building
	 */
	public Building(BuildingType type) throws RemoteException{
		initializeWithType(type);
	}
	
	private void initialize(BuildingType type, int costWood, int costSoil, int costIron, int costFood, int constructionTime, int life) {
		this.type = type;
		this.costWood = costWood;
		this.costSoil = costSoil;
		this.costIron = costIron;
		this.costFood = costFood;
		this.constructionTime = constructionTime;
		this.remainingTime = constructionTime;
		if(type.name().equals("Hotel")){
			this.status = 1;
			this.level = 1;
		}
		else{
			this.status = -1;
			this.level = 0;
		}		
	}
	
	private void initializeWithType(BuildingType type) {
		switch(type) {
			case Hotel:
				initialize(type, 70, 40, 60, 20, 30, 250);
				break;
			case Scierie:
				initialize(type, 520, 380, 290, 90, 45, 50);
				break;
			case Mine:
				initialize(type, 00, 80, 30, 60, 60, 60);
				break;
			case Ferme:
				initialize(type, 70, 90, 70, 20, 45, 30);
				break;
			case CARRIERE:
				initialize(type, 80, 40, 80, 50, 60, 60);
				break;
			case Entrepot:
				initialize(type, 130, 160, 90, 40, 120, 100);
				break;
			case Silo:
				initialize(type, 80, 100, 70, 20, 100, 80);
				break;
			case Caserne:
				initialize(type, 170, 200, 380, 130, 120, 150);
				break;
			case Ecurie:
				initialize(type, 220, 160, 90, 40, 200, 200);
				break;
		}
	}
	
	public int getConstructionTime() {
		return this.constructionTime;
	}
	
	/**
	 * get the necessary amount of food to built the building
	 * @return the amount of food
	 */
	public int getCostFood() {
		return this.costFood;
	}
	
	/**
	 * get the necessary amount of iron to built the building
	 * @return the amount of iron
	 */
	public int getCostIron() {
		return this.costIron;
	}
	
	/**
	 * get the necessary amount of soil to built the building
	 * @return the amount of soil
	 */
	public int getCostSoil() {
		return this.costSoil;
	}
	
	/**
	 * get the necessary amount of wood to built the building
	 * @return the amount of wood
	 */
	public int getCostWood() {
		return this.costWood;
	}
	
	/**
	 * get the level of the structure
	 * @return the level
	 */
	public int getLevel() {
		return this.level;
	}
	
	/**
	 * get the status of the building :
	 *  _ not built 
	 * 	- under construction
	 *  - built
	 * @return the status of the building
	 */
	public String getStatus() {
		switch(status){
			case -1: return "Non construit";
			case 0: return "En construction";
			default: return "Construit";
		}
	}
	
	/**
	 * get the type of the building
	 * @return the type of the building
	 */
	public BuildingType getType() {
		return this.type;
	}
	
	public void constructBuilding(){
				
		this.status = 0;
		
		this.remainingTime = this.constructionTime;
		
		BuildingThread compteur = new BuildingThread();
		compteur.setBuildling(this);
		compteur.start();
	}
	
	public int getRemainingTime(){
		return this.remainingTime;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	/**
	 * @param level the level to set
	 */
	public void setLevel(int level) {
		this.level = level;
	}

	/**
	 * @param costWood the costWood to set
	 */
	public void setCostWood(int costWood) {
		this.costWood = costWood;
	}

	/**
	 * @param costSoil the costSoil to set
	 */
	public void setCostSoil(int costSoil) {
		this.costSoil = costSoil;
	}

	/**
	 * @param costIron the costIron to set
	 */
	public void setCostIron(int costIron) {
		this.costIron = costIron;
	}

	/**
	 * @param costFood the costFood to set
	 */
	public void setCostFood(int costFood) {
		this.costFood = costFood;
	}

	/**
	 * @param constructionTime the constructionTime to set
	 */
	public void setConstructionTime(int constructionTime) {
		this.constructionTime = constructionTime;
	}

	/**
	 * @param remainingTime the remainingTime to set
	 */
	public void setRemainingTime(int remainingTime) {
		this.remainingTime = remainingTime;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(BuildingType type) {
		this.type = type;
	}
}